﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OAuth.ServiceProviders;
using System.Diagnostics;
using OAuth;
using System.Net;
using System.IO;
using YammerTestApp.Properties;
using YammerAPI;

namespace YammerTestApp
{
    static class Program
    {
        internal static string AuthUserWithResponse(string pCompletUrl)
        {
            Process.Start(pCompletUrl);
            Console.WriteLine("User is being redirected to '{0}'", WebUtils.UrlShorteners.rim.Instance.Shorten(pCompletUrl));
            Console.Write("Enter the Code from the website: ");
            return Console.ReadLine();
        }

        internal static IAccessTokenStorage TransitoryTokenStorage = new TokenSettingsStorage();

        static string ConsumerKey
        {
            //get { return "ym2pUoszQMHrRinIiWQ0g"; }
            //get { return "3HLCKYlqH4WSwQRdJpmcSQ"; }
            get { return "cGp6TWVRuYZpKdiEMwYd2Q"; }
        }

        static string ConsumerSecret
        {
            //get { return "eVXjAYGMZKqD9JTa5YoVZgHHHtDqLFFXxALyUfsQ1c"; }
            //get { return "DB8VHY50K4GXCyy2BKjr9eWxppJc51It3gymVPriw8"; }
            get { return "7LpyLO06kwPPYVC1NkwkPzMZFck9HZyV0TBkFa7gSc"; }
        }

        static void Main(string[] args)
        {
            log4net.Util.LogLog.InternalDebugging = true;
            log4net.Config.XmlConfigurator.Configure(new FileInfo("log4netconfig.xml"));

            var api = new YammerAPI.ApiWrapper(
                ConsumerKey,
                ConsumerSecret,
                TransitoryTokenStorage,
                AuthUserWithResponse);
            
            api.StartSession(false);
            api.PostMessage("test", null);
            return;

            foreach(var msg in (from m in (api.GetAllMessages().Reverse<Message>())
                                select new
                                {
                                    Username = (from user in m.References.Users where user.Id == m.SenderId select user.FullName).First(),
                                    shortName = (from user in m.References.Users where user.Id == m.SenderId select user.Name).First(),
                                    Message = m.Body.Plain,
                                    Time = m.CreatedAt
                                }))
            {
                string.Format("From   : {0} ({1}) [{2}]\n", msg.Username, msg.shortName, msg.Time).Red();
                string.Format("Message: {0}\n", msg.Message).Blue();
                Console.WriteLine();
            }

            Console.WriteLine("Pressy any key...");
            Console.ReadKey(true);
        }
    }

    static class Utilities
    {
        public static void Red(this string pString)
        {
            ConsoleColor old = Console.ForegroundColor;
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write(pString);
            Console.ForegroundColor = old;
        }
        
        public static void Blue(this string pString)
        {
            ConsoleColor old = Console.ForegroundColor;
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.Write(pString);
            Console.ForegroundColor = old;
        }
    }
}
